﻿using CloudDcPc.Entity;
using CloudDcPc.Util;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CloudDcPc
{
    /// <summary>
    /// OrdersAllDlg.xaml 的交互逻辑
    /// </summary>
    public partial class OrdersAddDlg : Window
    {
        private ObservableCollection<MyMenu> list_left = new ObservableCollection<MyMenu>();
        private ObservableCollection<MyMenu> list_right = new ObservableCollection<MyMenu>();

        private string order_num;
        public OrdersAddDlg(string order_num)
        {
            InitializeComponent();
            this.order_num = order_num;

            List<string> list_food_type = new List<string>();
            list_food_type = App.MyMenuBean.GetAllType();
            cb_food_type.ItemsSource = list_food_type;


            list_left = App.MyMenuBean.GetMyMenusByType(cb_food_type.Text);
            list_left_xml.ItemsSource = list_left;
            list_right_xml.ItemsSource = list_right;
        }

        private void cb_food_type_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            list_left = App.MyMenuBean.GetMyMenusByType(cb_food_type.SelectedItem+"");
            list_left_xml.ItemsSource = list_left;
        }

        private void btn_2right_Click(object sender, RoutedEventArgs e)
        {
            foreach (MyMenu item in list_left_xml.SelectedItems)
            {
                if (list_right.IndexOf(item) < 0)
                    list_right.Add(item);
            }
            list_left_xml.SelectedIndex = -1;
        }
        
        private void btn_2left_Click(object sender, RoutedEventArgs e)
        {
            List<MyMenu> list = new List<MyMenu>();
            //遍历过程中不能修改故...
            foreach (MyMenu item in list_right_xml.SelectedItems)
            {
                list.Add(item);
            }
            foreach (MyMenu item in list)
            {
                list_right.Remove(item);
            }
            list_right_xml.SelectedIndex = -1;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            
            string postStr = "",result = "";
            int count = list_right.Count;
            bool flag = false;
            if (count > 0)
            {
                postStr += "{\"menulist\":[";
                for (int i = 0; i < count; i++)
                {
                    MyMenu item = list_right[i];

                    try
                    {
                        float.Parse(item.CountInTg);
                    }
                    catch
                    {
                        MessageBox.Show("数量必须填数字！！");
                        return;
                    }

                    if (float.Parse(item.CountInTg) > 0)
                    {
                        if (item.Suffix == "")
                            item.Suffix = "null";
                        TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);


                        postStr += "{\"time\":\"" + Convert.ToInt64(ts.TotalMilliseconds).ToString() + "\",\"suffix\":\"" + item.Suffix + "\",\"unit\":\"" + item.Unit + "\",\"foodnum\":\"" + item.Food_num + "\",\"type\":\"" + item.Type + "\",\"printpos\":" + item.Print_pos + ",\"piece\":\"" + item.CountInTg + "\",\"name\":\"" + item.Food_name + "\"},";
                        //  if(i!=count-1 && list_right[i+1].CountInTg>0){

                        //  }
                        flag = true;
                    }
                    else
                    {
                        MessageBox.Show("数量必须大于零！！");
                        return;
                    }
                }

                postStr = postStr.TrimEnd(',');

                postStr += "],\"tabnum\":" + App.OrderBean.GetOrderByOrderNum(order_num).Tab_num + "}";
            }

            if (flag)
            {
                try{
                        PostSubmitter postSub = new PostSubmitter();
                    result = postSub.SimplePost(postStr,"http://localhost:8080/OrderSystemServlet/AddOrderServlet");
                    }catch{
                        MessageBox.Show("与服务器通讯发生错误！");
                        return;
                    }
                if (result.IndexOf('1') > 0)
                {
                    MessageBox.Show("加菜成功！");
                    this.DialogResult = true;
                }
                else
                {
                    MessageBox.Show("加菜失败！");
                    return;
                }
            }
            else
            {
                MessageBox.Show("未选择菜品或选择的菜品数量都为空！");
                return;
            }

        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
